package org.jeecg.modules.pos.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.pos.entity.PosThali;
import org.jeecg.modules.pos.entity.PosThaliInGoods;
import org.jeecg.modules.pos.service.IPosThaliInGoodsService;
import org.jeecg.modules.pos.service.IPosThaliService;
import org.jeecg.modules.rooms.entity.CesGoods;
import org.jeecg.modules.rooms.service.CesGoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: pos_thali
 * @Author: jeecg-boot
 * @Date:   2023-04-11
 * @Version: V1.0
 */
@Api(tags="pos_thali")
@RestController
@RequestMapping("/pos/posThali")
@Slf4j
public class PosThaliController extends JeecgController<PosThali, IPosThaliService> {
	@Autowired
	private IPosThaliService posThaliService;
	@Autowired
	private IPosThaliInGoodsService posThaliInGoodsService;
	/**
	 * 分页列表查询
	 *
	 * @param posThali
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "pos_thali-分页列表查询")
	@ApiOperation(value="pos_thali-分页列表查询", notes="pos_thali-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<PosThali>> queryPageList(PosThali posThali,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PosThali> queryWrapper = QueryGenerator.initQueryWrapper(posThali, req.getParameterMap());
		Page<PosThali> page = new Page<PosThali>(pageNo, pageSize);
		IPage<PosThali> pageList = posThaliService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param posThali
	 * @return
	 */
	@AutoLog(value = "pos_thali-添加")
	@ApiOperation(value="pos_thali-添加", notes="pos_thali-添加")
	//@RequiresPermissions("pos:pos_thali:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody PosThali posThali) {
		if (posThali.getTenantId() == null || posThali.getTenantId().equals("")) {
			LoginUser user = TokenUtils.getAuthUser();
			if (user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")) {
				posThali.setTenantId(user.getRelTenantIds());
			} else {
				throw new JeecgBootException("当前登录人租户信息错误");
			}
		}
		posThali.setId(UUIDGenerator.generate());
		CesGoods cesGoods = BeanUtil.copyProperties(posThali, CesGoods.class);
		cesGoods.setId(null);
		boolean res = posThaliService.save(posThali);
		if (res) {
			if (ObjectUtils.isNotEmpty(posThali.getPosThaliInGoodsList())) {
				posThali.getPosThaliInGoodsList().forEach(t -> {
					t.setGoodsId(t.getId());
					t.setId("");
					t.setHotelId(posThali.getHotelId());
					t.setPosThaliId(posThali.getId());
					t.setTenantId(TokenUtils.currentTenantId());
				});
				posThaliInGoodsService.saveBatch(posThali.getPosThaliInGoodsList());
			}
		}
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param posThali
	 * @return
	 */
	@AutoLog(value = "pos_thali-编辑")
	@ApiOperation(value="pos_thali-编辑", notes="pos_thali-编辑")
	//@RequiresPermissions("pos:pos_thali:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody PosThali posThali) {
		Boolean res = posThaliService.updateById(posThali);
		if (res) {
			LambdaQueryWrapper<PosThaliInGoods> lambdaQueryWrapper=new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(PosThaliInGoods::getPosThaliId,posThali.getId());
			posThaliInGoodsService.remove(lambdaQueryWrapper);
			if (ObjectUtils.isNotEmpty(posThali.getPosThaliInGoodsList())) {
				posThali.getPosThaliInGoodsList().forEach(t -> {
					t.setGoodsId(t.getId());
					t.setId("");
					t.setHotelId(posThali.getHotelId());
					t.setPosThaliId(posThali.getId());
					t.setTenantId(TokenUtils.currentTenantId());
				});
				posThaliInGoodsService.saveBatch(posThali.getPosThaliInGoodsList());
			}
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "pos_thali-通过id删除")
	@ApiOperation(value="pos_thali-通过id删除", notes="pos_thali-通过id删除")
	//@RequiresPermissions("pos:pos_thali:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		posThaliService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "pos_thali-批量删除")
	@ApiOperation(value="pos_thali-批量删除", notes="pos_thali-批量删除")
	//@RequiresPermissions("pos:pos_thali:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.posThaliService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "pos_thali-通过id查询")
	@ApiOperation(value="pos_thali-通过id查询", notes="pos_thali-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<PosThali> queryById(@RequestParam(name="id",required=true) String id) {
		PosThali posThali = posThaliService.getById(id);
		if(posThali==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(posThali);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param posThali
    */
    //@RequiresPermissions("pos:pos_thali:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PosThali posThali) {
        return super.exportXls(request, posThali, PosThali.class, "pos_thali");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("pos:pos_thali:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PosThali.class);
    }

}
